package com.wq.concurrency.aqs;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: wangqiang20995
 * @Date:2019/1/30
 * @Description:
 * @Resource:
 */
public class LockDemo {

    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        AtomicReference<Thread> lockThread = new AtomicReference<>();

        Thread interruptThread = null;
        for(int i = 0;i<5;i++){
            Thread thread = new Thread(() -> {

               reentrantLock.lock();
               try{
                   //将获取到lock的线程暴露给外部
                   lockThread.set(Thread.currentThread());
                   Thread.sleep(1000000000);
               }catch (Exception e){
                   System.out.println("["+Thread.currentThread()+"] exception --> " + e.getMessage());
               }finally {
                   reentrantLock.unlock();
               }
            });

            thread.start();

            if(i == 2){
                interruptThread = thread;
            }
        }

        Thread.sleep(3000);

        //主动将获得锁的线程中断
        lockThread.get().interrupt();

        //将在AQS阻塞队列中等待的一个node中断
        interruptThread.interrupt();
    }
}
